﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace TeamFoundation.ContinuousMerge.Plugins
{
    /// <summary>
    /// Represents a plugin.
    /// </summary>
    public class Plugin : IDisposable
    {
        private volatile bool _isInitialized;
        /// <summary>
        /// Gets a value indicating whether the plugin has been initialized.
        /// </summary>
        public bool IsInitialized
        {
            get
            {
                return _isInitialized;
            }
        }

        private volatile bool _isDisposed;
        /// <summary>
        /// Gets a value indicating whether the plugin has been disposed.
        /// </summary>
        public bool IsDisposed
        {
            get
            {
                return _isDisposed;
            }
        }

        /// <summary>
        /// Gets the plugin runtime.
        /// </summary>
        public Runtime Runtime
        {
            get;
            private set;
        }

        /// <summary>
        /// Gets the configuration data.
        /// </summary>
        protected internal XDocument Configuration
        {
            get;
            internal set;
        }

        /// <summary>
        /// Initializes the plugin.
        /// </summary>
        /// <param name="runtime">The parent runtime.</param>
        public void Initialize(Runtime runtime)
        {
            if (_isInitialized)
                return;
            _isInitialized = true;

            Runtime = runtime;
            OnInitialize();
        }

        /// <summary>
        /// Initializes the plugin.
        /// </summary>
        protected virtual void OnInitialize()
        {
        }

        #region IDisposable Members

        /// <summary>
        /// Finalizer.
        /// </summary>
        ~Plugin()
        {
            if (_isDisposed)
                return;
            _isDisposed = true;

            Dispose(false);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Disposes of the object and any related resources.
        /// </summary>
        public void Dispose()
        {
            if (_isDisposed)
                return;
            _isDisposed = true;

            Dispose(true);
        }

        /// <summary>
        /// Disposes of the object and any related resources.
        /// </summary>
        /// <param name="disposing">Whether the dispose method was invoked (true) or the finalizer was invoked (false).</param>
        protected virtual void Dispose(bool disposing)
        {

        }

        #endregion
    }
}
